package com.nature.yanglegeyangultimate.container;

import com.nature.yanglegeyangultimate.domain.Card;
import com.nature.yanglegeyangultimate.util.ReadResourcesUtil;

import java.util.*;

/**
 * 全局的卡牌容器
 */
public class AllControlContainer {

    //记录当前初始化牌堆总牌数的静态常值变量
    public static int CARD_NUMBER;

    //记录翻牌区卡牌和中央区卡牌的常量
    public static int CARD_NUMBER_CENTER;
    public static int CARD_NUMBER_REVERSE;

    //当前的难度系数，以牌%3的倍数作为难度系数，初始难度系数为1
    private static final int HARD_PARAMS = 1;

    //设置中心牌区单层的卡牌数量
    public static final int CARD_NUMBER_SINGULAR_LEVEL = 10;

    //初始的全局牌盒容器
    public List<Card> originalList = new ArrayList<>();

    //中心牌盒容器
    public CenterCardContainer centerCardContainer;

    //翻转牌盒容器
    public ReverseCardContainer reverseCardContainer;

    //验卡牌盒容器
    public CheckCardContainer checkCardContainer;

    //基准坐标参数信息
    private static final int WIDTH = 850;
    private static final int LARGE_RECT_X = 32; // 起始x坐标
    private static final int LARGE_RECT_Y = 660; // 起始y坐标，高度的1/4
    private static final int LARGE_RECT_WIDTH = WIDTH - 80; // 宽度与绿色边界相近
    private static final int CARD_WIDTH = 80;
    private static final int CARD_HEIGHT = 100;

    //初始化一个随机数对象辅助
    private static final Random random = new Random();

    //牌区点击事件标记变量
    private static final int CARD_LEFT_FLAG = 2;
    private static final int CARD_RIGHT_FLAG = 3;


    public AllControlContainer() {
        //初始化三个容器
        centerCardContainer = new CenterCardContainer();
        reverseCardContainer = new ReverseCardContainer();
        checkCardContainer = new CheckCardContainer();

        //初始化全局的原始牌盒容器
        initOriginalList();

        //翻牌区牌盒左右分牌
        initReverseContainer();

        //初始化中心牌盒容器
        initCenterContainer();
    }

    //卡牌流转综合方法
    public boolean cardTransfer(int level, Card clickedCard, int flag) {
        boolean result = false;
        if (level == 3) {
            //说明点击的是翻牌区的卡牌
            result = cardTransferReverse2Check(flag, clickedCard);
        } else {
            //说明点击的是中心区卡牌
            result = cardTransferCenter2Check(level, clickedCard);
        }
        return result;
    }

    //卡牌流转，从翻牌区到验卡区，翻牌区的牌只从两端开始移除，所以需要判断左右
    private boolean cardTransferReverse2Check(int flag, Card clickedCard) {
        boolean result = false;
        if (flag == CARD_LEFT_FLAG && clickedCard.getCardId() == reverseCardContainer.cardLeft.get(0).getCardId() && checkCardContainer.isNotFull()) {
            //移动左边翻牌区的第一张牌到验卡区
            Card card = reverseCardContainer.cardLeft.remove(0);
//            System.out.println("要删除的卡牌对象" + card);
            result = checkCardContainer.addCard(card);
        } else if (flag == CARD_RIGHT_FLAG && clickedCard.getCardId() == reverseCardContainer.cardRight.get(0).getCardId() && checkCardContainer.isNotFull()) {
            //移动右边翻牌区的第一张牌到验卡区
            Card card = reverseCardContainer.cardRight.remove(0);
//            System.out.println("要删除的卡牌对象" + card);
            result = checkCardContainer.addCard(card);
        } else if (clickedCard.getCardId() != reverseCardContainer.cardLeft.get(0).getCardId()) {
            result = true;
        }
        //打印验卡区容器
        System.out.println("验卡区容器" + checkCardContainer.checkCardMap);
        return result;
    }

    //卡牌流转，从中心区到验卡区
    private boolean cardTransferCenter2Check(int level, Card clickedCard) {
        boolean result = false;
        //获取对应层级的集合list
        List<Card> levelList = centerCardContainer.centerCardMap.get(level - 4);
        //根据点击的牌的id检查在指定层级中是否由对应的卡牌
        if (levelList.contains(clickedCard) && checkCardContainer.isNotFull()) {
            //说明点击的牌是中心区的牌，将其从对应层级的list中移除，并添加到验卡区的map中
            levelList.remove(clickedCard);
            System.out.println("被移除的卡牌" + clickedCard);
            result = checkCardContainer.addCard(clickedCard);
        } else {
            System.out.println("点击的牌不存在");
        }
        return result;
    }

    //初始化中心卡牌区牌盒
    public void initCenterContainer() {
        //先获取中心牌区的原始牌盒
        List<Card> centerOriginList = centerCardContainer.centerOriginList;
        //获取按层存储的map集合
        Map<Integer, List<Card>> centerCardMap = centerCardContainer.centerCardMap;
        int level = 0;
        //遍历中心牌盒，将每层牌放入map集合中，先按照每层10张放置，最终剩下的牌的数量不足一层单独放置一层
        for (int i = 0; i < centerOriginList.size(); i++) {
            Card c = centerOriginList.get(i);
            initCardCenterHelper(c);
            if (i % CARD_NUMBER_SINGULAR_LEVEL == 0) {
                level++;
                List<Card> cards = new ArrayList<>();
                c.setLevel(level + 3);
                cards.add(c);
                centerCardMap.put(i / CARD_NUMBER_SINGULAR_LEVEL, cards);
            } else {
                c.setLevel(level + 3);
                List<Card> cards = centerCardMap.get(i / CARD_NUMBER_SINGULAR_LEVEL);
                cards.add(c);
            }
        }
//        System.out.println("中心区卡牌信息");
//        System.out.println(CARD_NUMBER_CENTER);
//        //遍历map，打印键和对应的list集合长度
//        for (Map.Entry<Integer, List<Card>> entry : centerCardMap.entrySet()) {
//            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
//        }
    }

    //中心牌盒初始化的辅助方法
    private void initCardCenterHelper(Card card) {
        //给每一个卡牌随机坐标
        //实际获取的大小确定： max(y)=430 min(y)=25  max(x)=653  min(x)=101
        //还剩下540*540的区域
        //测试一下实际的区域大小，这关系到后面的牌的坐标随机  max(y)=430 min(y)=25 inf(x)=32  sup(x)=720
        //实际获取的大小确定： max(y)=430 min(y)=25  max(x)=653  min(x)=101
        card.setXLeft(random.nextInt(553) + 101); // 生成101到653之间的随机x坐标
        card.setYUp(random.nextInt(406) + 25);    // 生成25到430之间的随机y坐标
        card.setXRight(card.getXLeft() + CARD_WIDTH);
        card.setYDown(card.getYUp() + CARD_HEIGHT);
    }

    //初始化翻牌区的牌盒
    public void initReverseContainer() {
        //翻牌区牌盒左右分牌
        List<Card> reverseOriginalList = reverseCardContainer.reverseOriginalList;
        for (int i = 0; i < reverseOriginalList.size(); i++) {
            if (i % 2 == 0) {
                reverseCardContainer.cardLeft.add(reverseOriginalList.get(i));
            } else {
                reverseCardContainer.cardRight.add(reverseOriginalList.get(i));
            }
        }
        initCardReverseHelper("left", reverseCardContainer.cardLeft);
        initCardReverseHelper("right", reverseCardContainer.cardRight);
        System.out.println("翻牌区卡牌信息");
        System.out.println(reverseCardContainer.cardLeft);
        System.out.println(reverseCardContainer.cardRight);
    }

    //辅助初始化翻牌区牌盒的坐标和鼠标点击事件
    private void initCardReverseHelper(String side, List<Card> sideList) {
        //1.确定左侧翻牌区域的基准点坐标
        int x = side.equals("left") ? LARGE_RECT_X : LARGE_RECT_X + LARGE_RECT_WIDTH - CARD_WIDTH;
        int y = LARGE_RECT_Y - 10 - CARD_HEIGHT;

        //2.获取卡牌的数量 todo 暂时设置为30
        int n = sideList.size();
        //3.获取左侧区域的大小以便获取每一个圆角矩形的左上坐标
        int width = (LARGE_RECT_WIDTH - CARD_WIDTH) / (3 * n);
        for (int i = n - 1; i >= 0; i--) {
            int tempX = side.equals("left") ? x + i * width : x - i * width;
            Card c = sideList.get(i);
            c.setXLeft(tempX);
            c.setYUp(y);
            c.setXRight(tempX + CARD_WIDTH);
            c.setYDown(y + CARD_HEIGHT);
        }
    }

    //初始化原始牌盒容器的方法
    public void initOriginalList() {
        //1.渲染全局牌盒
        //获取到本轮要渲染的牌皮肤集合
        List<String> imagePath = ReadResourcesUtil.getImagePath(false);
        //初始化每张卡牌的id，以备后用
        int index = 0;
        for (String image : imagePath) {
            for (int i = 0; i < 3 * HARD_PARAMS; i++) {
                originalList.add(new Card(index, image));
                index++;
            }
        }
        //记录总牌数
        CARD_NUMBER = originalList.size();
        //同时打乱所有牌
        Collections.shuffle(originalList);

        //2.分牌
        for (int i = 0; i < CARD_NUMBER; i++) {
            if (i % 3 == 0) {
                //添加至翻牌初始牌盒
                reverseCardContainer.reverseOriginalList.add(originalList.get(i));
            } else {
                //添加至中心牌区牌盒
                centerCardContainer.centerOriginList.add(originalList.get(i));
            }
        }
        //记录分区牌数
        CARD_NUMBER_REVERSE = reverseCardContainer.reverseOriginalList.size();
        CARD_NUMBER_CENTER = centerCardContainer.centerOriginList.size();
    }
}
